package com.easylinkin.linkappapi.facerecognition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.facerecognition.entity.Personnel;
import com.easylinkin.linkappapi.facerecognition.entity.PersonnelIdentifier;
import com.easylinkin.linkappapi.facerecognition.mapper.PersonnelIdentifierMapper;
import com.easylinkin.linkappapi.facerecognition.service.PersonnelIdentifierService;
import com.easylinkin.linkappapi.facerecognition.service.PersonnelService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PersonnelIdentifierServiceImpl extends ServiceImpl<PersonnelIdentifierMapper, PersonnelIdentifier> implements PersonnelIdentifierService {

    @Resource
    LinkappUserContextProducer linkappUserContextProducer;

    @Autowired
    PersonnelService personnelService;

    @Override
    public PersonnelIdentifier queryWithIdentifierName(String identifierName) {

        QueryWrapper<PersonnelIdentifier> qw = new QueryWrapper<>();
        qw.eq("identifier", identifierName);
        qw.eq("tenant_id", linkappUserContextProducer.getNotNullCurrent().getTenantId());
        qw.eq("deleted", false);

        return baseMapper.selectOne(qw);
    }

    @Override
    public IPage<PersonnelIdentifier> queryPersonnelList(Page<PersonnelIdentifier> page, PersonnelIdentifier identifier) {

        QueryWrapper<PersonnelIdentifier> qw = new QueryWrapper<>();
        qw.eq("tenant_id", linkappUserContextProducer.getNotNullCurrent().getTenantId());
        qw.eq("deleted", false);
        if (identifier.getIdentifier() != null && !identifier.getIdentifier().trim().isEmpty()) {
            qw.like("identifier", "%" + identifier.getIdentifier().trim() + "%");
        }
        qw.orderByDesc("update_time");

        IPage<PersonnelIdentifier> personnelIdentifierIPage = baseMapper.selectPage(page, qw);
        List<Long> identifierIds = personnelIdentifierIPage.getRecords().stream().map(PersonnelIdentifier::getId).collect(Collectors.toList());

        if (!identifierIds.isEmpty()) {
            Map<Long, List<Personnel>> collect = personnelService.listByIdentifierIds(identifierIds)
                    .stream().collect(Collectors.groupingBy(Personnel::getIdentifierId));
            personnelIdentifierIPage.getRecords().forEach(idf -> {
                int count = collect.getOrDefault(idf.getId(), new ArrayList<>()).size();
                idf.setCount(count);
            });
        }

        return personnelIdentifierIPage;
    }
}
